﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Web.App_Codes
{
    public class CityRequest
    {

        /// <summary>
        /// 获取绵白糖城市信息
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public static List<TabInfo> GetCityTabsFromWeb(string code, string tableName, List<string> filters, int limit, string orderby, string priceColumn)
        {
            object obj = CacheManager.GetCache(code);
            if (obj == null)
            {
                var list = GetCityTabs(code, tableName, filters, limit, orderby, priceColumn);
                CacheManager.AddCache(code, list, App_Codes.GetCount.CacheTime * 30);
                return list;
            }
            else
            {
                return obj as List<TabInfo>;
            }
        }


        /// <summary>
        /// 获取绵白糖城市信息
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        private static List<TabInfo> GetCityTabs(string code, string tableName, List<string> filters, int limit, string orderby, string priceColumn)
        {
            List<TabInfo> tabInfos = new List<TabInfo>();
            F10ParamEntity entity = new F10ParamEntity(2);
            //   entity.Filters = new List<string>();

            entity.TableName = tableName;
            entity.Prefix = "Base";
            //   entity.Filters.Add("PRLBDY-gt-int=0");
            //if (code.Equals("MBT"))
            //{
            //    entity.Filters.Add("PTNAME-str=绵白糖");
            //}
            //else
            //{
            //    entity.Filters.Add("PTNAME-str=白砂糖");
            //}
            string[] price = priceColumn.Split(new char[] { ',' });
            entity.Filters = filters;
            entity.Filters.Add(price[0] + "-gt-int=0");
            entity.Limit = limit;

            string[] keys = orderby.Split(new char[] { ',' });

            string _sort = "";
            if (keys.Length > 1)
            {
                foreach (string key in keys)
                {
                    if (!string.IsNullOrEmpty(key))
                    {
                        _sort += key + " DESC,";
                    }
                }
            }
            else
            {
                _sort = orderby + " DESC";
            }

            entity.OrderBy = _sort;

            Newtonsoft.Json.Linq.JToken token;



            int totalCount;


            bool result = F10Query.TryParse(entity, false, out token, out totalCount);

            if (result)
            {
                for (int i = 0; i < token.Count(); i++)
                {
                    TabInfo info = new TabInfo();
                    int t = 0;
                    info.Code = token[i].J_Integer("UNICNATRG", t).ToString();
                    var query = tabInfos.Where(a => a.Code.Equals(info.Code)).Count();
                    if (query == 0)
                    {
                        info.Name = token[i].J_String("AREACNNAME");
                        info.PriceColumn = priceColumn;
                        tabInfos.Add(info);
                    }
                }
            }
            tabInfos = tabInfos.OrderBy(a => a.Code).ToList();
            return tabInfos;
        }

    }
}